package com.xqq.lottery.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.DataFormatException;

import com.xqq.lottery.bean.MessageItem;
import com.xqq.lottery.util.Util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.Settings.Global;
import android.util.Log;

public class NetUtil {
	
	private static final String TAG = "NetUtil";
	
	/**
	 * 检查是否有网络
	 * @return
	 */
	public static boolean checkNet(Context context){
		
		
		boolean isWIFI = isWIFIConnection(context);
		
		boolean isMOBILE = isMOBILEConnection(context);
		
		if(isMOBILE){
			readAPN(context);   //判断是哪个APN被选中了
		}
	    
		if(!isWIFI && !isMOBILE){
			return false;
		}
		
		//判断:WIFI连接
		//判断:Mobile连接
		//如果Mobile连接，判断是哪个APN被选中了
		//APN被选中了，代理信息是否有内容，如果有wap方式
		return true;
	}

	private static void readAPN(Context context) {
		Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = resolver.query(PREFERRED_APN_URI, null, null, null, null);
	    if(cursor != null && cursor.moveToFirst()){
	    	String proxy =  cursor.getString(cursor.getColumnIndex("proxy"));
	    	int port = cursor.getInt(cursor.getColumnIndex("port"));
	    	GloablParams.PROXY = proxy;
	    	GloablParams.PORT = port;
	    }
	}
	
	public static String GetNowAPN(Context context){
		ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if(networkInfo != null ){
			String network = networkInfo.getExtraInfo();
			boolean isMOBILE = isMOBILEConnection(context);
			if(isMOBILE){
				readAPN(context);   //判断是哪个APN被选中了
			}
			return network;
		}
		return null;
	}

	/**
	 * 判断Mobile连接
	 * @param context
	 * @return
	 */
	private static boolean isMOBILEConnection(Context context) {
		ConnectivityManager conManager =  (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo= conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if(networkInfo != null){
			return networkInfo.isConnected();
		}
		return false;
	}

	/**
	 * 判断WIFI连接
	 * @param context
	 * @return
	 */
	private static boolean isWIFIConnection(Context context) {
		ConnectivityManager conManager =  (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo= conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if(networkInfo != null){
			return networkInfo.isConnected();
		}
		return false;
	}
	
	
	/**
	 * 读取数据
	 * @param iStream
	 * @return
	 * @throws IOException
	 * @throws DataFormatException 
	 */
	public static MessageItem readData(InputStream iStream) throws IOException, DataFormatException{
		DataInputStream dis =new DataInputStream(iStream);
		int dataLen = dis.readInt();
		int dataType = dis.readInt();
		Log.e(TAG, "收到消息: dataLen="+dataLen+"dataType="+dataType);
		if(dataLen < 0 || dataLen > 65536 || dataType < 0 || dataType >65536){
			throw new DataFormatException("接收数据异常！");
		}
		byte[] content = new byte[dataLen];
		dis.read(content, 0, dataLen);

		MessageItem msgItem =new MessageItem();
		msgItem.setMsgType(dataType);
		msgItem.setMsgContent(content);
		return msgItem;
	}
	
	public static void sendData(OutputStream oStream,MessageItem msgItem) throws IOException{
		byte [] msgContent = msgItem.getMsgContent();
		DataOutputStream dos =new DataOutputStream(oStream);
		if(msgContent == null){
			return;
		}
		int len = msgContent.length;
		//写入数据长度
		dos.writeInt(len);
		dos.writeInt(msgItem.getMsgType());
		//写入内容
		oStream.write(msgContent);
		oStream.flush();
	}
	
	/**
	 * 读取数据包的长度，读一个int
	 * @param isStream
	 * @param len
	 * @return
	 * @throws IOException 
	 */
	public static int readDataLength(InputStream isStream,int len) throws IOException{
		byte [] data =new byte[len];
		int remainderLen = len;    //剩余没有读的字节
		while(remainderLen > 0){
			int retLen = isStream.read(data,len-remainderLen , remainderLen);
			if(retLen == -1){
				return -1;
			}else if(retLen > 0){
				remainderLen = remainderLen-retLen;
			}
		}
		return Util.bytesToInt(data, 0);
	}
	
	/**
	 * 
	 * @param isStream
	 * @param len
	 * @return 读取数据长度
	 * @throws IOException
	 */
	private static byte[] readDataContent(InputStream isStream ,int len) throws IOException{
		byte [] data =new byte[len];
		int readLen = len;    //剩余没有读的字节
		while(readLen > 0){
			int retLen = isStream.read(data,len-readLen , readLen);
			if(retLen == -1){
				return null;
			}else if(retLen > 0){
				readLen = readLen-retLen;
			}
		}
		return data;
	}
}
